package com.uenpay.oem.widget.listener;

import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextWatcher;
import android.widget.EditText;

/**
 * Author: liulou
 * Date: 2017/6/27.
 * desc: 输入金额EditText只允许到分
 */

public class AmountEditTextWatcher implements TextWatcher {

    EditText editText;

    public AmountEditTextWatcher(EditText et) {
        this.editText = et;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        editText.setFilters(new InputFilter[]{new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if (dest.toString().length() > 7) {
                    return "";
                }
                if (source.equals(".") && dest.toString().length() == 0) {
                    return "0.";
                }
                if (dest.toString().contains(".")) {
                    int index = dest.toString().indexOf(".");
                    int mlength = dest.toString().substring(index).length();
                    if (mlength == 3) {
                        return "";
                    }
                }
                return null;
            }
        }});
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}
